package com.easygem.front.service.imp;

import com.easygem.common.model.front.HighSeasClient;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.utils.DepUtils;
import com.easygem.common.vo.front.HighSeasClientVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.mapper.DepartmentMapper;
import com.easygem.front.mapper.HighSeasClientMapper;
import com.easygem.front.service.HighSeasClientService;
import com.easygem.front.service.base.AbstractServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author luoxi
 * @version 1.0
 * @className HighSeasClientServiceImpl
 * @Description 公海业务层实现类
 * @date 2024/12/23 15:36
 */
@Service

public class HighSeasClientServiceImpl extends AbstractServiceImpl<HighSeasClientMapper, HighSeasClient, Integer> implements HighSeasClientService {

    @Autowired
    private HighSeasClientMapper highSeasClientMapper;

    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 多条件联表查询公海客户
     * @param pageNum
     * @param pageSize
     * @param highSeasClientVO
     * @return
     */
    @Override
    public PageVO<HighSeasClient> findList(Integer pageNum, Integer pageSize, HighSeasClientVO highSeasClientVO) {
        //获取当前登录用户对象
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getDeptId());
        PageHelper.startPage(pageNum,pageSize);
        HighSeasClient highSeasClient = new HighSeasClient();
        BeanUtils.copyProperties(highSeasClientVO,highSeasClient);
//        highSeasClient.setIds(ids);
//        highSeasClient.setAccountName(activeUser.getUser().getAccountName());
        List<HighSeasClient> highSeasClientList = highSeasClientMapper.selectHighSeasClientListByHighSeasClient(highSeasClient);
        PageInfo<HighSeasClient> pageInfo = new PageInfo<>(highSeasClientList);
        return new PageVO<>(pageInfo.getTotal(),highSeasClientList);
    }
    /**
     * 将资源的删除状态变为1
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        int result = highSeasClientMapper.deleteResourceInfo(id);
        return result;
    }

//    /**
//     * 判断用户今日公海客户领取量是否大于系统规定
//     * @return
//     */
//    public int isExceedHighSeasClien(){
//        //获取当前登录用户对象
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        Long userId = activeUser.getUser().getUserId();
//        int result = highSeasClientMapper.isExceedHighSeasClien(userId);
//        return result;
//    }

    /**
     * 清除用户今日领取公海客户数量
     * @return
     */
    public int clearUsersHighSeasCount(){
        int result = highSeasClientMapper.clearUsersHighSeasCount();
        return result;
    }

    /**
     * 将用户今日领取公海客户数量+1
     * @return
     */
    public int usersHighSeasCountAccumulate(){
        //获取当前登录用户对象
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long userId = activeUser.getUser().getUserId();
        int result = highSeasClientMapper.usersHighSeasCountAccumulate(userId);
        return result;
    }

    /**
     * 将公海客户拉取为用户资源
     * @param id
     * @return
     */
    @Override
    public int updateUserId(Long id) {
        //获取当前登录用户对象
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();

        List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getUserId());
        if (!ids.isEmpty()) {
            Long userId = ids.get(0);
            int result = highSeasClientMapper.updateUserId(userId, id);
            return result;
        } else {
            // 根据业务逻辑处理ids为空的情况，例如返回特定值或者抛出异常
            return -1;
        }
    }

    /**
     * 删除公海客户的记录，使资源不再是公海客户
     * @param id
     * @return
     */
    @Override
    public int deleteHighSeasClientById(Integer id) {
        int result = highSeasClientMapper.deleteByHighSeasClientId(id);
        return result;
    }

    /**
     * 全部导出
     * @return
     */
    @Override
    public List<HighSeasClient> findAll() {
        //获取当前登录用户对象
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();

//        List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getDeptId());

//        highSeasClient.setIds(ids);
//        highSeasClient.setAccountName(activeUser.getUser().getAccountName());
//        System.out.println("99"+ids);
        // 根据部门ID列表查询客户列表
        List<HighSeasClient> highSeasClients = highSeasClientMapper.findAll();
        System.out.println("cccccccccccccc"+highSeasClients);
        for (HighSeasClient client : highSeasClients) {
            Integer isCall = client.getIsCall();
            if(isCall != null){
                String isCallStr = isCall == 0? "否" : (isCall == 1? "是" : "");
                client.setIsCallStr(isCallStr);
            }



            Integer lifeCycle = client.getLifeCycle();
            if(lifeCycle != null){
                String lifeCycleStr;
                switch (lifeCycle) {
                    case 0:
                        lifeCycleStr = "潜在";
                        break;
                    case 1:
                        lifeCycleStr = "签约";
                        break;
                    case 2:
                        lifeCycleStr = "重复购买";
                        break;
                    case 3:
                        lifeCycleStr = "失效";
                        break;
                    default:
                        lifeCycleStr = "";
                }
                client.setLifeCycleStr(lifeCycleStr);
            }

        }
        return highSeasClients;
    }

    /**
     * 批量导出
     * @param ids
     * @return
     */
    @Override
    public List<HighSeasClient> findAllByIds(List<Long> ids) {
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        List<Long> departmentIds = DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(), activeUser.getUser().getDeptId());
        HighSeasClient highSeasClient = new HighSeasClient();
//        highSeasClient.setIds(departmentIds);
        // 根据部门ID列表和resourceId列表查询客户列表
        List<HighSeasClient> highSeasClients = highSeasClientMapper.findAllByResourceId(highSeasClient,ids);
        for (HighSeasClient client : highSeasClients) {
            Integer isCall = client.getIsCall();
            String isCallStr = isCall == 0? "否" : (isCall == 1? "是" : "");
            client.setIsCallStr(isCallStr);

            Integer lifeCycle = client.getLifeCycle();
            String lifeCycleStr;
            switch (lifeCycle) {
                case 0:
                    lifeCycleStr = "潜在";
                    break;
                case 1:
                    lifeCycleStr = "签约";
                    break;
                case 2:
                    lifeCycleStr = "重复购买";
                    break;
                case 3:
                    lifeCycleStr = "失效";
                    break;
                default:
                    lifeCycleStr = "";
            }
            client.setLifeCycleStr(lifeCycleStr);
        }

        return highSeasClients;
    }
    //转入公海
    @Override
    public Integer toHighSeas(Long[] resourceIds) {
        Timestamp toHighSeasTime = Timestamp.valueOf(LocalDateTime.now());
        return highSeasClientMapper.toHighSeas(resourceIds, toHighSeasTime);
    }
}

